/*
 * $Id: TestADIFLocation.m 71 2009-06-18 04:16:30Z jon@bway.net $
 * Chalk
 *
 * Created 5/23/09
 * Copyright Jon Gordon
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#import "TestADIFLocation.h"

#import "QEADIFLocation.h"

@implementation TestADIFLocation
- (void)testCreation
{
    QEADIFLocation *l = [[QEADIFLocation alloc] init];
    STAssertNotNil(l, @"Could not create instance of QEADIFLocation.");
    STAssertNil([l stringValue], @"Default initializer created location with non-nil stringValue.");
    [l release];
} // testCreation


#define VALID_STR_1 @"N043 02.440"
#define VALID_STR_1_DIRECTION 'N'
#define VALID_STR_1_DEGREES 43
#define VALID_STR_1_MINUTES 2.44
#define VALID_STR_1_MINUTES_SRC @"2.44"

#define VALID_STR_2 @"E000 00.000"
#define VALID_STR_3 @"W000 00.001"
#define VALID_STR_4 @"S180 00.000"
#define CORRECTABLE_STR_1 @"s180 00.000"
#define CORRECTED_STR_1 @"S180 00.000"
#define INVALID_STR_1 @"A111 11.111"
#define INVALID_STR_2 @"S180 00.001"
#define INVALID_STR_3 @"S181 00.000"
#define INVALID_STR_4 @"S181 00.001"
#define INVALID_STR_5 @"W180 00.0"
#define INVALID_STR_6 @"W180 00.0  "
#define INVALID_STR_7 @"W18 00.001"
#define INVALID_STR_8 @"W18 00001"
#define INVALID_STR_9 @"W018 60.00"
#define INVALID_STR_10 @"W018 88.888"
 
- (void)testStringValidation
{
    NSString *src = VALID_STR_1;
    STAssertTrue([QEADIFLocation isValidLocationString:&src],
                 @"Reported \"%@\" as invalid location source.", VALID_STR_1);
    STAssertTrue(src == VALID_STR_1,
                 @"Replaced correct source \"%@\" with \"%@\".",
                 VALID_STR_1, src);
    src = VALID_STR_2;
    STAssertTrue([QEADIFLocation isValidLocationString:&src],
                 @"Reported \"%@\" as invalid location source.", VALID_STR_2);
    STAssertTrue(src == VALID_STR_2,
                 @"Replaced correct source \"%@\" with \"%@\".",
                 VALID_STR_2, src);
    src = VALID_STR_3;
    STAssertTrue([QEADIFLocation isValidLocationString:&src],
                 @"Reported \"%@\" as invalid location source.", VALID_STR_3);
    STAssertTrue(src == VALID_STR_3,
                 @"Replaced correct source \"%@\" with \"%@\".",
                 VALID_STR_3, src);
    src = VALID_STR_4;
    STAssertTrue([QEADIFLocation isValidLocationString:&src],
                 @"Reported \"%@\" as invalid location source.", VALID_STR_4);
    STAssertTrue(src == VALID_STR_4,
                 @"Replaced correct source \"%@\" with \"%@\".",
                 VALID_STR_4, src);
    
    src = CORRECTABLE_STR_1;
    STAssertTrue([QEADIFLocation isValidLocationString:&src],
                 @"Reported \"%@\" as invalid location source.", CORRECTABLE_STR_1);
    STAssertTrue(src != CORRECTABLE_STR_1,
                 @"Failed to replace correctable source \"%@\".",
                 CORRECTABLE_STR_1);
    STAssertEqualObjects(src, CORRECTED_STR_1,
                         @"Failed to correct correctable source \"%@\" to \"%@\"; src is \"%@\"."
                         CORRECTABLE_STR_1, CORRECTED_STR_1, src);
    
    src = INVALID_STR_1;
    STAssertFalse([QEADIFLocation isValidLocationString:&src],
                  @"Reported \"%@\" as valid location source.", INVALID_STR_1);
    src = INVALID_STR_2;
    STAssertFalse([QEADIFLocation isValidLocationString:&src],
                  @"Reported \"%@\" as valid location source.", INVALID_STR_2);
    src = INVALID_STR_3;
    STAssertFalse([QEADIFLocation isValidLocationString:&src],
                  @"Reported \"%@\" as valid location source.", INVALID_STR_3);
    src = INVALID_STR_4;
    STAssertFalse([QEADIFLocation isValidLocationString:&src],
                  @"Reported \"%@\" as valid location source.", INVALID_STR_4);
    src = INVALID_STR_5;
    STAssertFalse([QEADIFLocation isValidLocationString:&src],
                  @"Reported \"%@\" as valid location source.", INVALID_STR_5);
    src = INVALID_STR_6;
    STAssertFalse([QEADIFLocation isValidLocationString:&src],
                  @"Reported \"%@\" as valid location source.", INVALID_STR_6);
    src = INVALID_STR_7;
    STAssertFalse([QEADIFLocation isValidLocationString:&src],
                  @"Reported \"%@\" as valid location source.", INVALID_STR_7);
    src = INVALID_STR_8;
    STAssertFalse([QEADIFLocation isValidLocationString:&src],
                  @"Reported \"%@\" as valid location source.", INVALID_STR_8);
    src = INVALID_STR_9;
    STAssertFalse([QEADIFLocation isValidLocationString:&src],
                  @"Reported \"%@\" as valid location source.", INVALID_STR_9);
    src = INVALID_STR_10;
    STAssertFalse([QEADIFLocation isValidLocationString:&src],
                  @"Reported \"%@\" as valid location source.", INVALID_STR_10);
} // testStringValidation


- (void)testInitializationWithString
{
    QEADIFLocation *l = [[QEADIFLocation alloc] initWithString:VALID_STR_1];
    STAssertNotNil(l, @"initWithString could not create instance of QEADIFLocation using source \"%@\".",
                   VALID_STR_1);
    STAssertEqualObjects([l stringValue], VALID_STR_1, 
                         @"stringValue of location \"%@\"; does not match legal source \"%@\"",
                         [l stringValue], VALID_STR_1);
    STAssertTrue([l direction] == VALID_STR_1_DIRECTION,
                 @"direction is '%C', should be '%C'.",
                 [l direction], VALID_STR_1_DIRECTION);
    STAssertEqualObjects([l degrees], [NSNumber numberWithInt:VALID_STR_1_DEGREES],
                         @"degrees is %@, should be %d.",
                         [l degrees], VALID_STR_1_DEGREES);
    STAssertEqualObjects([l minutes], [NSDecimalNumber decimalNumberWithString:VALID_STR_1_MINUTES_SRC],
                         @"minutes is %@, should be %@.",
                         [l minutes], [NSDecimalNumber decimalNumberWithString:VALID_STR_1_MINUTES_SRC]);
    [l release];
    
    l = [[QEADIFLocation alloc] initWithString:CORRECTABLE_STR_1];
    STAssertNotNil(l, @"initWithString could not create instance of QEADIFLocation using source \"%@\".",
                   CORRECTABLE_STR_1);
    STAssertEqualObjects([l stringValue], CORRECTED_STR_1, 
                         @"stringValue of location \"%@\", created from \"%@\", should be \"%@\".",
                         [l stringValue], CORRECTABLE_STR_1, CORRECTED_STR_1);
    [l release];
    
    l = [[QEADIFLocation alloc] initWithString:INVALID_STR_1];
    STAssertNotNil(l, @"Could not create instance of QEADIFLocation with initWithString.");
    STAssertNil([l stringValue], @"Invalid initializer string \"%@\" used to create location with non-null stringValue.", INVALID_STR_1);
    [l release];
    
    l = [[QEADIFLocation alloc] initWithString:INVALID_STR_2];
    STAssertNotNil(l, @"Could not create instance of QEADIFLocation with initWithString.");
    STAssertNil([l stringValue], @"Invalid initializer string \"%@\" used to create location with non-null stringValue.", INVALID_STR_2);
    [l release];
} // testInitializationWithString


- (void)testInitializationWithComponents
{
    QEADIFLocation *l = [[QEADIFLocation alloc] initWithDirection:VALID_STR_1_DIRECTION 
                                                          degrees:[NSNumber numberWithInt:VALID_STR_1_DEGREES]
                                                          minutes:[NSDecimalNumber decimalNumberWithString:VALID_STR_1_MINUTES_SRC]];
    STAssertNotNil(l,
                   @"Could not create instance of QEADIFLocation with direction '%C', degrees %@, and minutes %@.",
                   VALID_STR_1_DIRECTION,
                   [NSNumber numberWithInt:VALID_STR_1_DEGREES],
                   [NSDecimalNumber decimalNumberWithString:VALID_STR_1_MINUTES_SRC]);
    STAssertTrue([[l stringValue] isEqual:VALID_STR_1],
                         @"String value of location is \"%@\"; should be \"%@\".",
                         [l stringValue], VALID_STR_1);
    
    l = [[QEADIFLocation alloc] initWithScalarsDirection:VALID_STR_1_DIRECTION 
                                                 degrees:VALID_STR_1_DEGREES
                                                 minutes:VALID_STR_1_MINUTES];
    STAssertNotNil(l,
                   @"Could not create instance of QEADIFLocation from scalars with direction '%C', degrees %d, and minutes %f.",
                   VALID_STR_1_DIRECTION,
                   VALID_STR_1_DEGREES,
                   VALID_STR_1_MINUTES_SRC);
    STAssertTrue([[l stringValue] isEqual:VALID_STR_1],
                         @"String value of location is \"%@\"; should be \"%@\".",
                         [l stringValue], VALID_STR_1);
    
    [l release];
} // testInitializationWithComponents
@end
